package com.service;

import static com.def.GenericDef.ACTION_FLG_ADD;
import static com.def.GenericDef.ACTION_FLG_DELETE;
import static com.def.GenericDef.ACTION_FLG_QUERY;
import static com.def.GenericDef.ACTION_FLG_UPDATE;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IRetShipDpsDao;
import com.icim.service.ICIMBaseService;
import com.model.Ret_ship_dps;
import com.tx.Xpshpdps.XpshpdpsI;
import com.tx.Xpshpdps.XpshpdpsIA;
import com.tx.Xpshpdps.XpshpdpsO;
import com.tx.Xpshpdps.XpshpdpsOA;
import com.util.GUIDGenerator;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpshpdpsService")
public class XpshpdpsService extends ICIMBaseService{

	private static final String TRX_TYPE_OUT = "O";
	private static final String TRX_NAME = "XPSHPDPS";
	private Logger logger = Logger.getLogger(XpshpdpsService.class);
	@Autowired
	private XpshpdpsI inTrx;

	@Autowired
	private XpshpdpsO outTrx;

	@Autowired
	private IRetShipDpsDao retShipDpsDao;

	@Autowired
	private MqOpe mqOpe;

	public String subMainProc(String strInMsg) {
		try {
			logger.info("inTrx is:" + strInMsg);
			outTrx.setTrx_id(TRX_NAME);
			outTrx.setType_id(TRX_TYPE_OUT);
			inTrx = (XpshpdpsI) mqOpe.inStr2Obj(strInMsg, inTrx);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_QUERY:
				queryByCondition();
				break;
			case ACTION_FLG_ADD:
				addFnc();
				break;
			case ACTION_FLG_UPDATE:
				updateFnc();
				break;
			case ACTION_FLG_DELETE:
				deleteFnc();
				break;
			default:
				errActionFnc();
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				retShipDpsDao.flushCurSession();
				outTrx.setRtn_mesg(RETURN_MESG_OK);
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.getCause().toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			logger.info(mqOpe.obj2Str(outTrx));
		}
		return mqOpe.obj2Str(outTrx);
	}

	public void addFnc() {
		String uuid = GUIDGenerator.javaGUID();
		List<XpshpdpsIA> iaryList = inTrx.getIary();
		for(XpshpdpsIA iary: iaryList){
			Ret_ship_dps ret_ship_dps = new Ret_ship_dps();
			ret_ship_dps.setEvt_seq_id(uuid);
			ret_ship_dps.setWo_id_fk(iary.getWo_id_fk());
			ret_ship_dps.setPln_ship_year(iary.getPln_ship_year());
			ret_ship_dps.setPln_ship_month(iary.getPln_ship_month());
			ret_ship_dps.setPln_ship_date(iary.getPln_ship_date());
			ret_ship_dps.setPln_ship_date_seq(iary.getPln_ship_date_seq());
			ret_ship_dps.setPln_ship_qty(iary.getPln_ship_qty());
			ret_ship_dps.setPv_pln_ship_qty(iary.getPv_pln_ship_qty());
			ret_ship_dps.setEvt_cate("BULD");
			ret_ship_dps.setEvt_usr(iary.getEvt_usr());
			ret_ship_dps.setEvt_timestamp(Util.getCurrentTimestamp());
			retShipDpsDao.save(ret_ship_dps);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
//		queryByKey();
	}

	public void updateFnc() {
		List<XpshpdpsIA> iaryList = inTrx.getIary();
		for(XpshpdpsIA iary: iaryList){
			Ret_ship_dps ret_ship_dps = retShipDpsDao.get(iary.getEvt_seq_id());
			if(iary.getWo_id_fk()!=null){
				ret_ship_dps.setWo_id_fk(iary.getWo_id_fk());
			}
			if(iary.getPln_ship_year()!=null){
				ret_ship_dps.setPln_ship_year(iary.getPln_ship_year());
			}
			if(iary.getPln_ship_month()!=null){
				ret_ship_dps.setPln_ship_month(iary.getPln_ship_month());
			}
			if(iary.getPln_ship_date()!=null){
				ret_ship_dps.setPln_ship_date(iary.getPln_ship_date());
			}
			if(iary.getPln_ship_date_seq()!=null){
				ret_ship_dps.setPln_ship_date_seq(iary.getPln_ship_date_seq());
			}
			if(iary.getPln_ship_qty()!=null){
				ret_ship_dps.setPln_ship_qty(iary.getPln_ship_qty());
			}
			if(iary.getPv_pln_ship_qty()!=null){
				ret_ship_dps.setPv_pln_ship_qty(iary.getPv_pln_ship_qty());
			}
			ret_ship_dps.setEvt_cate("CHGE");
			ret_ship_dps.setEvt_usr(iary.getEvt_usr());
			ret_ship_dps.setEvt_timestamp(Util.getCurrentTimestamp());
			retShipDpsDao.update(ret_ship_dps);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void deleteFnc() {
		List<XpshpdpsIA> iaryList = inTrx.getIary();
		for(XpshpdpsIA iary: iaryList){
			Ret_ship_dps ret_ship_dps = retShipDpsDao.get(iary.getEvt_seq_id());
			retShipDpsDao.delete(ret_ship_dps);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void queryByCondition() {
		StringBuffer buffer = new StringBuffer();
		XpshpdpsIA iary = inTrx.getIary().get(0);
		List<XpshpdpsOA> oaryList = new  ArrayList<XpshpdpsOA>();
		buffer.append("FROM Ret_ship_dps where 1=1");
		if(iary.getWo_id_fk()!=null){
			buffer.append(" AND wo_id_fk='").append(iary.getWo_id_fk()).append("'");
		}
		if(iary.getEvt_seq_id()!=null){
			buffer.append(" AND seq_id_fk='").append(iary.getEvt_seq_id()).append("'");
		}
		List<Ret_ship_dps> retShipDpsList = retShipDpsDao.find(buffer.toString());
		for(Ret_ship_dps ret_ship_dps : retShipDpsList){
			XpshpdpsOA oary = new XpshpdpsOA();
			oary.setEvt_seq_id(ret_ship_dps.getEvt_seq_id());
			oary.setWo_id_fk(ret_ship_dps.getWo_id_fk());
			oary.setPln_ship_year(ret_ship_dps.getPln_ship_year());
			oary.setPln_ship_month(ret_ship_dps.getPln_ship_month());
			oary.setPln_ship_date(ret_ship_dps.getPln_ship_date());
			oary.setPln_ship_date_seq(ret_ship_dps.getPln_ship_date_seq());
			oary.setPln_ship_qty(ret_ship_dps.getPln_ship_qty());
			oary.setPv_pln_ship_qty(ret_ship_dps.getPv_pln_ship_qty());
			oary.setEvt_cate(ret_ship_dps.getEvt_cate());
			oary.setEvt_usr(ret_ship_dps.getEvt_usr());
			oary.setEvt_timestamp(ret_ship_dps.getEvt_timestamp());
			oaryList.add(oary);
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
	}

	public void queryByKey() {
		XpshpdpsIA iary = inTrx.getIary().get(0);
		List<XpshpdpsOA> oaryList = new  ArrayList<XpshpdpsOA>();
		XpshpdpsOA oary = new XpshpdpsOA();
		
		Ret_ship_dps ret_ship_dps= retShipDpsDao.get(iary.getEvt_seq_id());
		oary.setEvt_seq_id(ret_ship_dps.getEvt_seq_id());
		oary.setWo_id_fk(ret_ship_dps.getWo_id_fk());
		oary.setPln_ship_year(ret_ship_dps.getPln_ship_year());
		oary.setPln_ship_month(ret_ship_dps.getPln_ship_month());
		oary.setPln_ship_date(ret_ship_dps.getPln_ship_date());
		oary.setPln_ship_date_seq(ret_ship_dps.getPln_ship_date_seq());
		oary.setPln_ship_qty(ret_ship_dps.getPln_ship_qty());
		oary.setPv_pln_ship_qty(ret_ship_dps.getPv_pln_ship_qty());
		oary.setEvt_cate(ret_ship_dps.getEvt_cate());
		oary.setEvt_usr(ret_ship_dps.getEvt_usr());
		oary.setEvt_timestamp(ret_ship_dps.getEvt_timestamp());
		oaryList.add(oary);
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
	}

	public void errActionFnc() {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}

}
